#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace UTable.Objects.Controls
{
    public class FileViewSelector : DataTemplateSelector
    {
        private DataTemplate fileItemTemplate = null;

        private DataTemplate directoryItemTemplate = null;

        public FileViewSelector()
        {
            fileItemTemplate = new DataTemplate();
            FrameworkElementFactory fileFactory = new FrameworkElementFactory(typeof(FileItemView));
            fileItemTemplate.VisualTree = fileFactory;

            directoryItemTemplate = new DataTemplate();
            FrameworkElementFactory directoryFactory = new FrameworkElementFactory(typeof(DirectoryItemView));
            directoryItemTemplate.VisualTree = directoryFactory;
        }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item != null && item is FileItemData)
            {
                FileItemData data = item as FileItemData;
                if (System.IO.File.Exists(data.FilePath))
                {
                    return fileItemTemplate;
                }
                if (System.IO.Directory.Exists(data.FilePath))
                {
                    return directoryItemTemplate;
                }
            }
            return base.SelectTemplate(item, container);
        }
    }
}
